package com.vhall.component.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.order.IncomeMapper;
import com.vhall.component.entity.income.IncomeCreateDTO;
import com.vhall.component.entity.order.IncomeEntity;
import com.vhall.component.entity.order.vo.IncomeReqVo;
import com.vhall.component.service.order.IncomeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author guoxin.wang
 * @date 2021-07-05 14:40
 */
@Slf4j
@Service
public class IncomeServiceImpl implements IncomeService {

    private static final String BALANCE = "balance";

    @Autowired
    private IncomeMapper incomeMapper;

    @Override
    public Map<String, String> getInfo(IncomeReqVo reqVO) {
        Map<String, String> result = new HashMap<>(4);
        result.put(BALANCE, "");
        LambdaQueryWrapper<IncomeEntity> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isEmpty(reqVO.getAccountId())) {
            BigDecimal sum = incomeMapper.selectSum(reqVO.getAppId());
            if (sum != null) {
                result.put(BALANCE, sum.toString());
            }
        } else {
            wrapper.eq(IncomeEntity::getAppId, reqVO.getAppId())
                    .eq(IncomeEntity::getAccountId, reqVO.getAccountId())
                    .orderByDesc(IncomeEntity::getId)
                    .last(" limit 1 ");
            IncomeEntity incomeEntity = incomeMapper.selectOne(wrapper);
            if (incomeEntity != null && incomeEntity.getBalance() != null) {
                result.put(BALANCE, incomeEntity.getBalance().toString());
            }
        }
        return result;
    }

    @Override
    public Boolean create(IncomeCreateDTO reqVO) {
        boolean flag;
        LambdaQueryWrapper<IncomeEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IncomeEntity::getAppId, reqVO.getAppId())
                .eq(IncomeEntity::getAccountId, reqVO.getAccountId())
                .orderByDesc(IncomeEntity::getId)
                .last(" limit 1 ");
        IncomeEntity incomeEntity = incomeMapper.selectOne(wrapper);
        if (incomeEntity == null) {
            // 新增
            reqVO.setTotal(reqVO.getAmount());
            reqVO.setBalance(reqVO.getAmount());
            reqVO.setCreatedAt(LocalDateTime.now());
            reqVO.setUpdatedAt(LocalDateTime.now());
            if (reqVO.getStatus() == null) {
                reqVO.setStatus(0);
            }
            flag = incomeMapper.insert(reqVO) > 0;
        } else {
            // 修改
            BigDecimal totalOfDb = incomeEntity.getTotal();
            BigDecimal sum = totalOfDb.add(reqVO.getAmount()).setScale(2, RoundingMode.HALF_UP);
            IncomeEntity param = new IncomeEntity();
            param.setId(incomeEntity.getId());
            param.setTotal(sum);
            param.setUpdatedAt(LocalDateTime.now());
            flag = incomeMapper.updateById(param) > 0;
        }
        return flag;
    }

}
